{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 100 \u4e2a Python \u5c0f\u6280\u5de7\n",
    "\n",
    "\n",
    "\n",
    "**\u258d1\u3001for\u5faa\u73af\u4e2d\u7684else\u6761\u4ef6**\n",
    "\n",
    "\n",
    "\n",
    "\u8fd9\u662f\u4e00\u4e2afor-else\u65b9\u6cd5\uff0c\u5faa\u73af\u904d\u5386\u5217\u8868\u65f6\u4f7f\u7528else\u8bed\u53e5\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u4e0b\u9762\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6bd4\u5982\u6211\u4eec\u60f3\u68c0\u67e5\u4e00\u4e2a\u5217\u8868\u4e2d\u662f\u5426\u5305\u542b\u5947\u6570\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\uff0c\u904d\u5386\u67e5\u627e\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers = [2, 4, 6, 8, 1]\n",
    "\n",
    "for number in numbers:\n",
    "    if number % 2 == 1:\n",
    "        print(number)\n",
    "        break\n",
    "else:\n",
    "    print(\"No odd numbers\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u5982\u679c\u627e\u5230\u4e86\u5947\u6570\uff0c\u5c31\u4f1a\u6253\u5370\u8be5\u6570\u503c\uff0c\u5e76\u4e14\u6267\u884cbreak\u8bed\u53e5\uff0c\u8df3\u8fc7else\u8bed\u53e5\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u6ca1\u6709\u7684\u8bdd\uff0c\u5c31\u4e0d\u4f1a\u6267\u884cbreak\u8bed\u53e5\uff0c\u800c\u662f\u6267\u884celse\u8bed\u53e5\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d2\u3001\u4ece\u5217\u8868\u4e2d\u83b7\u53d6\u5143\u7d20\uff0c\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1, 2, 3, 4, 5]\n",
    "one, two, three, four, five = my_list\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d3\u3001\u4f7f\u7528heapq\u6a21\u5757\uff0c\u83b7\u53d6\u5217\u8868\u4e2dn\u4e2a\u6700\u5927\u6216\u6700\u5c0f\u7684\u5143\u7d20**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import heapq\n",
    "\n",
    "scores = [51, 33, 64, 87, 91, 75, 15, 49, 33, 82]\n",
    "\n",
    "print(heapq.nlargest(3, scores))  # [91, 87, 82]\n",
    "print(heapq.nsmallest(5, scores))  # [15, 33, 33, 49, 51]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d4\u3001\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570**\n",
    "\n",
    "\n",
    "\n",
    "\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 * \u53f7\uff0c\u63d0\u53d6\u5217\u8868\u4e2d\u6240\u6709\u7684\u5143\u7d20\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1, 2, 3, 4]\n",
    "\n",
    "print(my_list)  # [1, 2, 3, 4]\n",
    "print(*my_list)  # 1 2 3 4\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u5982\u6b64\u4fbf\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sum_of_elements(*arg):\n",
    "    total = 0\n",
    "    for i in arg:\n",
    "        total += i\n",
    "\n",
    "    return total\n",
    "\n",
    "\n",
    "result = sum_of_elements(*[1, 2, 3, 4])\n",
    "print(result)  # 10\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d5\u3001\u83b7\u53d6\u5217\u8868\u7684\u6240\u6709\u4e2d\u95f4\u5143\u7d20**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_, *elements_in_the_middle, _ = [1, 2, 3, 4, 5, 6, 7, 8]\n",
    "print(elements_in_the_middle)  # [2, 3, 4, 5, 6, 7]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d6\u3001****\u4f7f\u7528\u4e00\u884c\u4ee3\u7801\u8d4b\u503c\u591a\u4e2a\u53d8\u91cf**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "one, two, three, four = 1, 2, 3, 4\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d7\u3001\u5217\u8868\u63a8\u5bfc\u5f0f**\n",
    "\n",
    "\n",
    "\n",
    "\u53ea\u7528\u4e00\u884c\u4ee3\u7801\uff0c\u4fbf\u53ef\u5b8c\u6210\u5bf9\u6570\u7ec4\u7684\u8fed\u4ee3\u4ee5\u53ca\u8fd0\u7b97\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u6bd4\u5982\uff0c\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u63d0\u9ad8\u4e00\u500d\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers = [1, 2, 3, 4, 5]\n",
    "squared_numbers = [num * num for num in numbers]\n",
    "\n",
    "print(squared_numbers) # [1, 4, 9, 16, 25]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u5217\u8868\u80fd\u7528\uff0c\u5b57\u5178\u3001\u96c6\u5408\u3001\u751f\u6210\u5668\u4e5f\u80fd\u4f7f\u7528\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u4e0b\u9762\u770b\u4e00\u4e0b\uff0c\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u5c06\u5b57\u5178\u7684\u503c\u63d0\u9ad8\u4e00\u500d\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dictionary = {'a': 4, 'b': 5}\n",
    "squared_dictionary = {key: num * num for (key, num) in dictionary.items()}\n",
    "\n",
    "print(squared_dictionary)  # {'a': 16, 'b': 25}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d8\u3001\u901a\u8fc7Enum\u679a\u4e3e\u540c\u4e00\u6807\u7b7e\u6216\u4e00\u7cfb\u5217\u5e38\u91cf\u7684\u96c6\u5408**\n",
    "\n",
    "\n",
    "\n",
    "\u679a\u4e3e\u662f\u7ed1\u5b9a\u5230\u552f\u4e00\u7684\u5e38\u91cf\u503c\u7684\u4e00\u7ec4\u7b26\u53f7\u540d\u79f0(\u6210\u5458)\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u5728\u679a\u4e3e\u4e2d\uff0c\u6210\u5458\u53ef\u4ee5\u901a\u8fc7\u8eab\u4efd\u8fdb\u884c\u6bd4\u8f83\uff0c\u679a\u4e3e\u672c\u8eab\u53ef\u4ee5\u8fed\u4ee3\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from enum import Enum\n",
    "\n",
    "\n",
    "class Status(Enum):\n",
    "    NO_STATUS = -1\n",
    "    NOT_STARTED = 0\n",
    "    IN_PROGRESS = 1\n",
    "    COMPLETED = 2\n",
    "\n",
    "\n",
    "print(Status.IN_PROGRESS.name)  # IN_PROGRESS\n",
    "print(Status.COMPLETED.value)  # 2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d9\u3001****\u91cd\u590d\u5b57\u7b26\u4e32**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Banana\"\n",
    "print(name * 4)  # BananaBananaBananaBanana\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d10\u3001\u6bd4\u8f833\u4e2a\u6570\u5b57\u7684\u5927\u5c0f**\n",
    "\n",
    "\n",
    "\n",
    "\u5982\u679c\u60f3\u6bd4\u8f83\u4e00\u4e2a\u503c\u548c\u5176\u4ed6\u4e24\u4e2a\u503c\u7684\u5927\u5c0f\u60c5\u51b5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u6570\u5b66\u8868\u8fbe\u5f0f\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1 < x < 10\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd9\u4e2a\u662f\u6700\u7b80\u5355\u7684\u4ee3\u6570\u8868\u8fbe\u5f0f\uff0c\u5728Python\u4e2d\u4e5f\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 3\n",
    "\n",
    "print(1 < x < 10)  # True\n",
    "print(1 < x and x < 10)  # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d11\u3001\u4f7f\u75281\u884c\u4ee3\u7801\u5408\u5e76\u5b57\u5178**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_dictionary = {'name': 'Fan', 'location': 'Guangzhou'}\n",
    "second_dictionary = {'name': 'Fan', 'surname': 'Xiao', 'location': 'Guangdong, Guangzhou'}\n",
    "\n",
    "result = first_dictionary | second_dictionary\n",
    "\n",
    "print(result)\n",
    "# {'name': 'Fan', 'location': 'Guangdong, Guangzhou', 'surname': 'Xiao'}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d12\u3001\u67e5\u627e\u5143\u7ec4\u4e2d\u5143\u7d20\u7684\u7d22\u5f15**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "books = ('Atomic habits', 'Ego is the enemy', 'Outliers', 'Mastery')\n",
    "\n",
    "print(books.index('Mastery'))   # 3\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d13\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5217\u8868**\n",
    "\n",
    "\n",
    "\n",
    "\u5047\u8bbe\u4f60\u5728\u51fd\u6570\u4e2d\u83b7\u5f97\u8f93\u51fa\uff0c\u539f\u672c\u5e94\u8be5\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u4f46\u5b9e\u9645\u4e0a\u5374\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "input = \"[1,2,3]\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u4f60\u53ef\u80fd\u7b2c\u4e00\u65f6\u95f4\u4f1a\u60f3\u5230\u4f7f\u7528\u7d22\u5f15\u6216\u8005\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u5b9e\u9645\u4e0a\uff0c\u4f7f\u7528ast\u6a21\u5757\u7684literal_eval\u65b9\u6cd5\u5c31\u80fd\u641e\u5b9a\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ast\n",
    "\n",
    "\n",
    "def string_to_list(string):\n",
    "    return ast.literal_eval(string)\n",
    "\n",
    "\n",
    "string = \"[1, 2, 3]\"\n",
    "my_list = string_to_list(string)\n",
    "print(my_list)  # [1, 2, 3]\n",
    "\n",
    "string = \"[[1, 2, 3],[4, 5, 6]]\"\n",
    "my_list = string_to_list(string)\n",
    "print(my_list)  # [[1, 2, 3], [4, 5, 6]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d14\u3001****\u8ba1\u7b97\u4e24\u6570\u5dee\u503c**\n",
    "\n",
    "\n",
    "\n",
    "\u8ba1\u7b97\u51fa2\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def subtract(a, b):\n",
    "    return a - b\n",
    "\n",
    "\n",
    "print((subtract(1, 3)))  # -2\n",
    "print((subtract(3, 1)))  # 2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u4e0a\u9762\u7684\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u9700\u8981\u8003\u8651\u6570\u503c\u7684\u5148\u540e\u987a\u5e8f\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def subtract(a, b):\n",
    "    return a - b\n",
    "\n",
    "\n",
    "print((subtract(a=1, b=3)))  # -2\n",
    "print((subtract(b=3, a=1)))  # -2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u4f7f\u7528\u547d\u540d\u53c2\u6570\uff0c\u5b89\u6392\u987a\u5e8f\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u51fa\u9519\u4e86\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d15\u3001****\u7528\u4e00\u4e2aprint()\u8bed\u53e5\u6253\u5370\u591a\u4e2a\u5143\u7d20**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1, 2, 3, \"a\", \"z\", \"this is here\", \"here is something else\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d16\u3001****\u5728\u540c\u4e00\u884c\u6253\u5370\u591a\u4e2a\u5143\u7d20**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello\", end=\"\")\n",
    "print(\"World\")  # HelloWorld\n",
    "\n",
    "print(\"Hello\", end=\" \")\n",
    "print(\"World\")  # Hello World\n",
    "\n",
    "print('words',   'with', 'commas', 'in', 'between', sep=', ')\n",
    "# words, with, commas, in, between\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d17\u3001****\u6253\u5370\u591a\u4e2a\u503c\uff0c\u5728\u6bcf\u4e2a\u503c\u4e4b\u95f4\u4f7f\u7528\u81ea\u5b9a\u4e49\u5206\u9694\u7b26**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"29\", \"01\", \"2022\", sep=\"/\")  # 29/01/2022\n",
    "\n",
    "print(\"name\", \"domain.com\", sep=\"@\")  # name@domain.com\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d18\u3001****\u4e0d\u80fd\u5728\u53d8\u91cf\u540d\u7684\u5f00\u5934\u4f7f\u7528\u6570\u5b57**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "four_letters = \"abcd\" # this works\n",
    "\n",
    "4_letters = \"abcd\" # this doesn\u2019t work\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd9\u662fPython\u7684\u53d8\u91cf\u547d\u540d\u89c4\u5219\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d19\u3001\u4e0d\u80fd\u5728\u53d8\u91cf\u540d\u7684\u5f00\u5934\u4f7f\u7528\u8fd0\u7b97\u7b26**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "+variable = \"abcd\"  # this doesn\u2019t work\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d20\u3001****\u6570\u5b57\u7684\u7b2c\u4e00\u4f4d\u4e0d\u80fd\u662f0**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 0110 # this doesn't work\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd9\u4e2a\u786e\u5b9e\u633a\u795e\u5947\u7684\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d21\u3001****\u5728\u53d8\u91cf\u540d\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u4e0b\u5212\u7ebf**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a______b = \"abcd\"  # this works\n",
    "_a_b_c_d = \"abcd\"  # this also works\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\uff0c\u4f60\u53ef\u4ee5\u65e0\u9650\u4f7f\u7528\uff0c\u4e3a\u4e86\u4ee3\u7801\u7684\u6613\u8bfb\u6027\uff0c\u8fd8\u662f\u9700\u8981\u5408\u7406\u4f7f\u7528\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d22\u3001\u4f7f\u7528\u4e0b\u5212\u7ebf\u5206\u5272\u6570\u503c\u8f83\u5927\u7684\u6570\u5b57**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1_000_000_000)  # 1000000000\n",
    "print(1_234_567)  # 1234567\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u5982\u6b64\uff0c\u770b\u5230\u4e00\u5927\u5806\u6570\u5b57\u65f6\uff0c\u4e5f\u80fd\u8f7b\u677e\u9605\u8bfb\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d23\u3001\u53cd\u8f6c\u5217\u8868**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = ['a', 'b', 'c', 'd']\n",
    "\n",
    "my_list.reverse()\n",
    "\n",
    "print(my_list)  # ['d', 'c', 'b', 'a']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d24\u3001\u4f7f\u7528\u6b65\u8fdb\u51fd\u6570\u5bf9\u5b57\u7b26\u4e32\u5207\u7247**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_string = \"This is just a sentence\"\n",
    "print(my_string[0:5])  # This\n",
    "\n",
    "# Take three steps forward\n",
    "print(my_string[0:10:3])  # Tsse\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d25\u3001\u53cd\u5411****\u5207\u7247**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_string = \"This is just a sentence\"\n",
    "print(my_string[10:0:-1])  # suj si sih\n",
    "\n",
    "# Take two steps forward\n",
    "print(my_string[10:0:-2])  # sjs i\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d26\u3001\u4f7f\u7528\u5f00\u59cb\u6216\u7ed3\u675f\u7d22\u5f15\u8fdb\u884c\u5207\u7247**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_string = \"This is just a sentence\"\n",
    "print(my_string[4:])  # is just a sentence\n",
    "\n",
    "print(my_string[:3])  # Thi\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d27\u3001****/\u548c//\u7684\u533a\u522b**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(3/2)  # 1.5\n",
    "print(3//2)  # 1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d28\u3001==\u548cis\u7684\u533a\u522b**\n",
    "\n",
    "\n",
    "\n",
    "is\uff1a\u68c0\u67e5\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u5bf9\u8c61\u5185\u5b58\u4e2d\n",
    "\n",
    "==\uff1a\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_list = [1, 2, 3]\n",
    "second_list = [1, 2, 3]\n",
    "\n",
    "# \u6bd4\u8f83\u4e24\u4e2a\u503c\n",
    "print(first_list == second_list)  # True\n",
    "\n",
    "# \u662f\u5426\u6307\u5411\u540c\u4e00\u5185\u5b58\n",
    "print(first_list is second_list)  \n",
    "# False\n",
    "\n",
    "\n",
    "third_list = first_list\n",
    "\n",
    "print(third_list is first_list)  \n",
    "# True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d29\u3001\u5408\u5e76\u5b57\u5178**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dictionary_one = {\"a\": 1, \"b\": 2}\n",
    "dictionary_two = {\"c\": 3, \"d\": 4}\n",
    "\n",
    "merged = {**dictionary_one, **dictionary_two}\n",
    "\n",
    "print(merged)  # {'a': 1, 'b': 2, 'c': 3, 'd': 4}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d30\u3001\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5927\u4e8e\u53e6\u4e00\u5b57\u7b26\u4e32**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first = \"abc\"\n",
    "second = \"def\"\n",
    "\n",
    "print(first < second)  # True\n",
    "\n",
    "second = \"ab\"\n",
    "print(first < second)  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d31\u3001\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u7279\u5b9a\u5b57\u7b26\u5f00\u5934(\u4e0d\u4f7f\u7528\u7d22\u5f15)**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_string = \"abcdef\"\n",
    "print(my_string.startswith(\"b\"))  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d32\u3001\u4f7f\u7528id()\u67e5\u627e\u53d8\u91cf\u7684\u552f\u4e00id**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(id(1))  # 4325776624\n",
    "print(id(2))  # 4325776656\n",
    "print(id(\"string\"))  # 4327978288\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d33\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u548c\u5143\u7ec4\u90fd\u662f\u4e0d\u53ef\u53d8\u7684**\n",
    "\n",
    "\n",
    "\n",
    "\u5f53\u53d8\u91cf\u88ab\u8d4b\u503c\u4e3a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u3001\u5143\u7ec4\u8fd9\u4e9b\u4e0d\u53ef\u53d8\u7c7b\u578b\u540e\uff0c\u8be5\u53d8\u91cf\u5c31\u4f1a\u6307\u5411\u4e00\u4e2a\u5185\u5b58\u5bf9\u8c61\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u5982\u679c\u91cd\u65b0\u7ed9\u53d8\u91cf\u518d\u8d4b\u503c\uff0c\u5b83\u7684\u5185\u5b58\u5bf9\u8c61\u5c31\u4f1a\u53d1\u751f\u6539\u53d8\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 1\n",
    "print(id(number))  # 4325215472\n",
    "print(id(1))  # 4325215472\n",
    "\n",
    "number = 3\n",
    "print(id(number))  # 4325215536\n",
    "print(id(1))  # 4325215472\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d34\u3001\u5b57\u7b26\u4e32\u548c\u5143\u7ec4\u4e5f\u662f\u4e0d\u53ef\u53d8\u7684**\n",
    "\n",
    "\n",
    "\n",
    "\u6b64\u5904\u518d\u8bf4\u660e\u4e00\u6b21\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Fatos\"\n",
    "print(id(name))  # 4422282544\n",
    "\n",
    "name = \"fatos\"\n",
    "print(id(name))  # 4422346608\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d35\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u5b57\u5178\u90fd\u662f\u53ef\u53d8\u7684**\n",
    "\n",
    "\n",
    "\n",
    "\u8fd9\u610f\u5473\u7740\u53d1\u751f\u66f4\u6539\u65f6\uff0c\u4e0d\u4f1a\u6539\u53d8\u5176\u5185\u5b58\u5bf9\u8c61\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cities = [\"Beijing\", \"Guangzhou\", \"chengdu\"]\n",
    "print(id(cities))  # 4482699712\n",
    "\n",
    "cities.append(\"Beijing\")\n",
    "print(id(cities))  # 4482699712\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u4e0b\u9762\u662f\u5b57\u5178\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_set = {1, 2, 3, 4}\n",
    "print(id(my_set))  # 4352726176\n",
    "\n",
    "my_set.add(5)\n",
    "print(id(my_set))  # 4352726176\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d36\u3001\u628a\u4e00\u4e2a\u5217\u8868\u53d8\u6210\u4e0d\u53ef\u53d8\u7684\u5217\u8868**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_set = frozenset(['a', 'b', 'c', 'd'])\n",
    "\n",
    "my_set.add(\"a\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u4f7f\u7528frozenset()\u540e\uff0c\u4f60\u5c31\u65e0\u6cd5\u66f4\u6539\u4e86\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d37\u3001if-elif\u5757\u53ef\u4ee5\u5728\u6ca1\u6709else\u5757\u7684\u60c5\u51b5\u4e0b\u5b58\u5728**\n",
    "\n",
    "\n",
    "\n",
    "\u4f46\u662felif\u4e0d\u80fd\u5728\u6ca1\u6709if\u8bed\u53e5\u4e4b\u524d\u72ec\u7acb\u5b58\u5728\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check_number(number):\n",
    "    if number > 0:\n",
    "        return \"Positive\"\n",
    "    elif number == 0:\n",
    "        return \"Zero\"\n",
    "\n",
    "    return \"Negative\"\n",
    "\n",
    "\n",
    "print(check_number(1))  # Positive\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d38\u3001\u4f7f\u7528sorted()\u68c0\u67e52\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u76f8\u540c**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check_if_anagram(first_word, second_word):\n",
    "    first_word = first_word.lower()\n",
    "    second_word = second_word.lower()\n",
    "    return sorted(first_word) == sorted(second_word)\n",
    "\n",
    "\n",
    "print(check_if_anagram(\"testinG\", \"Testing\"))  # True\n",
    "print(check_if_anagram(\"Here\", \"Rehe\"))  # True\n",
    "print(check_if_anagram(\"Know\", \"Now\"))  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d39\u3001\u83b7\u53d6\u5b57\u7b26\u7684Unicode\u503c**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(ord(\"A\"))  # 65\n",
    "print(ord(\"B\"))  # 66\n",
    "print(ord(\"C\"))  # 66\n",
    "print(ord(\"a\"))  # 97\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d40\u3001\u83b7\u53d6\u5b57\u5178\u7684\u952e**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dictionary = {\"a\": 1, \"b\": 2, \"c\": 3}\n",
    "\n",
    "keys = dictionary.keys()\n",
    "print(list(keys))  # ['a', 'b', 'c']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d41\u3001\u83b7\u53d6\u5b57\u5178\u7684\u503c**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dictionary = {\"a\": 1, \"b\": 2, \"c\": 3}\n",
    "\n",
    "values = dictionary.values()\n",
    "print(list(values))  # [1, 2, 3]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d42\u3001\u4ea4\u6362\u5b57\u5178\u7684\u952e\u3001\u503c\u4f4d\u7f6e**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dictionary = {\"a\": 1, \"b\": 2, \"c\": 3}\n",
    "\n",
    "reversed_dictionary = {j: i for i, j in dictionary.items()}\n",
    "print(reversed)  # {1: 'a', 2: 'b', 3: 'c'}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d43\u3001\u5c06\u5e03\u5c14\u503c\u8f6c\u6362\u4e3a\u6570\u5b57**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(int(False))  # 0\n",
    "print(float(True))  # 1.0\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d44\u3001\u5728\u7b97\u672f\u8fd0\u7b97\u4e2d\u4f7f\u7528\u5e03\u5c14\u503c**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 10\n",
    "y = 12\n",
    "\n",
    "result = (x - False)/(y * True)\n",
    "print(result)  # 0.8333333333333334\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d45\u3001\u5c06\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(bool(.0))  # False\n",
    "print(bool(3))  # True\n",
    "print(bool(\"-\"))  # True\n",
    "print(bool(\"string\"))  # True\n",
    "print(bool(\" \"))  # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d46\u3001\u5c06\u503c\u8f6c\u6362\u4e3a\u590d\u6570**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(complex(10, 2))  # (10+2j)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u4e5f\u53ef\u4ee5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u6570\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(hex(11))  # 0xb\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d47\u3001\u5728\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u6dfb\u52a0\u4e00\u4e2a\u503c**\n",
    "\n",
    "\n",
    "\n",
    "\u5982\u679c\u4f7f\u7528append()\uff0c\u5c06\u4ece\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u63d2\u5165\u65b0\u503c\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528insert()\uff0c\u6765\u6307\u5b9a\u63d2\u5165\u65b0\u5143\u7d20\u7684\u7d22\u5f15\u548c\u6570\u503c\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u90a3\u4e48\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u4e3a0\uff0c\u5373\u4e0b\u6807\u4e3a0\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [3, 4, 5]\n",
    "\n",
    "my_list.append(6)\n",
    "my_list.insert(0, 2)\n",
    "print(my_list)  # [2, 3, 4, 5, 6]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d48\u3001Lambda\u51fd\u6570\u53ea\u80fd\u5728\u4e00\u884c\u4ee3\u7801\u4e2d**\n",
    "\n",
    "\n",
    "\n",
    "\u65e0\u6cd5\u901a\u8fc7\u591a\u884c\u4ee3\u7801\uff0c\u6765\u4f7f\u7528lambda\u51fd\u6570\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "comparison = lambda x: if x > 3:\n",
    "                    print(\"x > 3\")\n",
    "                else:\n",
    "                    print(\"x is not greater than 3\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u62a5\u9519\u3002\n",
    "\n",
    "\n",
    "\n",
    "![\u56fe\u7247](https://mmbiz.qpic.cn/mmbiz_png/y0SBuxfLhakdhAhYOXSibK8SicSPnaGPh1nTicSISTwEv8WAREB1pgfZhibsj1IxYgx7rMnfwMkYB36HTRuG1mOkEQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d49\u3001Lambda\u4e2d\u7684\u6761\u4ef6\u8bed\u53e5\u5e94\u59cb\u7ec8\u5305\u542belse\u8bed\u53e5**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "comparison = lambda x: \"x > 3\" if x > 3\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u62a5\u9519\u3002\n",
    "\n",
    "\n",
    "\n",
    "![\u56fe\u7247]()\n",
    "\n",
    "\n",
    "\n",
    "\u8fd9\u662f\u7531\u4e8e\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u7279\u6027\uff0c\u800c\u4e0d\u662flambda\u7684\u5bfc\u81f4\u7684\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d50\u3001\u4f7f\u7528filter()\uff0c\u83b7\u5f97\u4e00\u4e2a\u65b0\u5bf9\u8c61**\n",
    "\n",
    "**\n",
    "**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1, 2, 3, 4]\n",
    "\n",
    "odd = filter(lambda x: x % 2 == 1, my_list)\n",
    "\n",
    "print(list(odd))   # [1, 3]\n",
    "print(my_list)  # [1, 2, 3, 4]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d51\u3001map****()\u8fd4\u56de\u4e00\u4e2a\u65b0\u5bf9\u8c61**\n",
    "\n",
    "\n",
    "\n",
    "map()\u51fd\u6570\u5c06\u7ed9\u5b9a\u51fd\u6570\u5e94\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61(\u5217\u8868\u3001\u5143\u7ec4\u7b49)\uff0c\u7136\u540e\u8fd4\u56de\u7ed3\u679c(map\u5bf9\u8c61)\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1, 2, 3, 4]\n",
    "\n",
    "squared = map(lambda x: x ** 2, my_list)\n",
    "\n",
    "print(list(squared))   # [1, 4, 9, 16]\n",
    "print(my_list)  # [1, 2, 3, 4]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d52\u3001range()\u7684step\u53c2\u6570**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for number in range(1, 10, 3):\n",
    "    print(number, end=\" \")\n",
    "# 1 4 7\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d53\u3001range()\u9ed8\u8ba4\u4ece0\u5f00\u59cb**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def range_with_zero(number):\n",
    "    for i in range(0, number):\n",
    "        print(i, end=' ')\n",
    "\n",
    "\n",
    "def range_with_no_zero(number):\n",
    "    for i in range(number):\n",
    "        print(i, end=' ')\n",
    "\n",
    "\n",
    "range_with_zero(3)  # 0 1 2\n",
    "range_with_no_zero(3)  # 0 1 2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d54\u3001\u4e0d\u9700\u8981\u548c0\u6bd4\u8f83\u957f\u5ea6**\n",
    "\n",
    "\n",
    "\n",
    "\u5982\u679c\u957f\u5ea6\u5927\u4e8e0\uff0c\u5219\u9ed8\u8ba4\u4e3aTrue\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_element_with_comparison(my_list):\n",
    "    if len(my_list) > 0:\n",
    "        return my_list[0]\n",
    "\n",
    "\n",
    "def get_first_element(my_list):\n",
    "    if len(my_list):\n",
    "        return my_list[0]\n",
    "\n",
    "\n",
    "elements = [1, 2, 3, 4]\n",
    "first_result = get_element_with_comparison(elements)\n",
    "second_result = get_element_with_comparison(elements)\n",
    "\n",
    "print(first_result == second_result)  # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d55\u3001\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u5185\u591a\u6b21\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5**\n",
    "\n",
    "\n",
    "\n",
    "\u4f46\u662f\uff0c\u53ea\u6709\u6700\u540e\u4e00\u4e2a\u4f1a\u88ab\u8c03\u7528\uff0c\u8986\u76d6\u4ee5\u524d\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_address():\n",
    "    return \"First address\"\n",
    "\n",
    "\n",
    "def get_address():\n",
    "    return \"Second address\"\n",
    "\n",
    "\n",
    "def get_address():\n",
    "    return \"Third address\"\n",
    "\n",
    "\n",
    "print(get_address())  # Third address\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d56\u3001\u5728\u5916\u90e8\u76f4\u63a5\u8bbf\u95ee\u79c1\u6709\u5c5e\u6027**\n",
    "\n",
    "\n",
    "\n",
    "\u5728\u5b9a\u4e49\u5c5e\u6027\u6216\u65b9\u6cd5\u65f6\uff0c\u5728\u5c5e\u6027\u540d\u6216\u8005\u65b9\u6cd5\u540d\u524d\u589e\u52a0\u4e24\u4e2a\u4e0b\u5212\u7ebf\uff0c\u5b9a\u4e49\u7684\u5c31\u662f\u79c1\u6709\u5c5e\u6027\u6216\u65b9\u6cd5\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u5982\u679c\u60f3\u8981\u5728\u5916\u90e8\u8bbf\u95ee\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u5728\u540d\u79f0\u524d\u9762\u52a0\u4e0a '_\u7c7b\u540d' \u53d8\u6210 '_\u7c7b\u540d__\u540d\u79f0'\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Engineer:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        self.__starting_salary = 62000\n",
    "\n",
    "\n",
    "dain = Engineer('Dain')\n",
    "print(dain._Engineer__starting_salary)  # 62000\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d57\u3001\u68c0\u67e5\u5bf9\u8c61\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "print(sys.getsizeof(\"bitcoin\"))  # 56\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d58\u3001\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8c03\u7528\u4efb\u610f\u4e2a\u53c2\u6570**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_sum(*arguments):\n",
    "    result = 0\n",
    "    for i in arguments:\n",
    "        result += i\n",
    "    return result\n",
    "\n",
    "\n",
    "print(get_sum(1, 2, 3))  # 6\n",
    "print(get_sum(1, 2, 3, 4, 5))  # 15\n",
    "print(get_sum(1, 2, 3, 4, 5, 6, 7))  # 28\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d59\u3001\u4f7f\u7528super()\u6216\u7236\u7c7b\u7684\u540d\u79f0\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316**\n",
    "\n",
    "\n",
    "\n",
    "\u4f7f\u7528super\u51fd\u6570\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Parent:\n",
    "    def __init__(self, city, address):\n",
    "        self.city = city\n",
    "        self.address = address\n",
    "\n",
    "\n",
    "class Child(Parent):\n",
    "    def __init__(self, city, address, university):\n",
    "        super().__init__(city, address)\n",
    "        self.university = university\n",
    "\n",
    "\n",
    "child = Child('Peking University', 'Fudan University', 'Tsinghua University')\n",
    "print(child.university)  # Tsinghua University\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u4f7f\u7528\u7236\u7c7b\u7684\u540d\u79f0\u8c03\u7528\u7236\u7c7b\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Parent:\n",
    "    def __init__(self, city, address):\n",
    "        self.city = city\n",
    "        self.address = address\n",
    "\n",
    "\n",
    "class Child(Parent):\n",
    "    def __init__(self, city, address, university):\n",
    "        Parent.__init__(self, city, address)\n",
    "        self.university = university\n",
    "\n",
    "\n",
    "child = Child('Peking University', 'Fudan University', 'Tsinghua University')\n",
    "print(child.university)  # Tsinghua University\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d60\u3001\u5728\u7c7b\u4e2d\u4f7f\u7528 + \u64cd\u4f5c\u7b26**\n",
    "\n",
    "\n",
    "\n",
    "\u5728\u4e24\u4e2aint\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u4f7f\u7528 + \u8fd0\u7b97\u7b26\u65f6\uff0c\u5c06\u5f97\u5230\u5b83\u4eec\u7684\u548c\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u800c\u5728\u4e24\u4e2a\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u4f7f\u7528\u5b83\u65f6\uff0c\u4f1a\u5c06\u5176\u5408\u5e76\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(10 + 1)  # \u4e24\u6570\u76f8\u52a0\n",
    "print('first' + 'second')  # \u5b57\u7b26\u4e32\u76f8\u52a0\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd9\u4e2a\u5c31\u662f\u64cd\u4f5c\u7b26\u91cd\u8f7d\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5728\u7c7b\u4e2d\u4f7f\u7528(__add__)\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Expenses:\n",
    "    def __init__(self, rent, groceries):\n",
    "        self.rent = rent\n",
    "        self.groceries = groceries\n",
    "\n",
    "    def __add__(self, other):\n",
    "        return Expenses(self.rent + other.rent,\n",
    "                        self.groceries + other.groceries)\n",
    "\n",
    "\n",
    "april_expenses = Expenses(1000, 200)\n",
    "may_expenses = Expenses(1000, 300)\n",
    "\n",
    "total_expenses = april_expenses + may_expenses\n",
    "print(total_expenses.rent)  # 2000\n",
    "print(total_expenses.groceries)  # 500\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d61\u3001\u5728\u7c7b\u4e2d\u4f7f\u7528 < \u548c == \u64cd\u4f5c\u7b26**\n",
    "\n",
    "\n",
    "\n",
    "\u4e0b\u9762\u5b9a\u4e49\u4e00\u4e2a\u64cd\u4f5c\u91cd\u8f7d\u793a\u4f8b( < \u64cd\u4f5c\u7b26)\uff0c\u4f7f\u7528__lt__\u65b9\u6cd5\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Game:\n",
    "    def __init__(self, score):\n",
    "        self.score = score\n",
    "\n",
    "    def __lt__(self, other):\n",
    "        return self.score < other.score\n",
    "\n",
    "\n",
    "first = Game(1)\n",
    "second = Game(2)\n",
    "\n",
    "print(first < second)  # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u540c\u6837\u7684\uff0c== \u64cd\u4f5c\u7b26\u4f7f\u7528__eq__\u65b9\u6cd5\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Journey:\n",
    "    def __init__(self, location, destination, duration):\n",
    "        self.location = location\n",
    "        self.destination = destination\n",
    "        self.duration = duration\n",
    "\n",
    "    def __eq__(self, other):\n",
    "        return ((self.location == other.location) and\n",
    "                (self.destination == other.destination) and\n",
    "                (self.duration == other.duration))\n",
    "\n",
    "\n",
    "first = Journey('Location A', 'Destination A', '30min')\n",
    "second = Journey('Location B', 'Destination B', '30min')\n",
    "\n",
    "print(first == second)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u5b9a\u4e49\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "__sub__() for -\n",
    "__mul__() for *\n",
    "__truediv__() for /\n",
    "__ne__() for !=\n",
    "__ge__() for >=\n",
    "__gt__() for >\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d62\u3001\u4e3a\u7c7b\u7684\u5bf9\u8c61\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u53ef\u6253\u5370\u7248\u672c**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Rectangle:\n",
    "    def __init__(self, a, b):\n",
    "        self.a = a\n",
    "        self.b = b\n",
    "\n",
    "    def __repr__(self):\n",
    "        return repr('Rectangle with area=' + str(self.a * self.b))\n",
    "\n",
    "\n",
    "print(Rectangle(3, 4))  # 'Rectangle with area=12'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d63\u3001\u4ea4\u6362\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u5927\u5c0f\u5199**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"This is just a sentence.\"\n",
    "result = string.swapcase()\n",
    "\n",
    "print(result)  # tHIS IS JUST A SENTENCE.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d64\u3001\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u90fd\u662f\u7a7a\u683c**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"  \"\n",
    "result = string.isspace()\n",
    "\n",
    "print(result)  # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d65\u3001\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u90fd\u662f\u5b57\u6bcd\u6216\u6570\u5b57**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Password\"\n",
    "print(name.isalnum())  # True\n",
    "\n",
    "name = \"Secure Password \"\n",
    "print(name.isalnum())  # False\n",
    "\n",
    "name = \"S3cur3P4ssw0rd\"\n",
    "print(name.isalnum())  # True\n",
    "\n",
    "name = \"133\"\n",
    "print(name.isalnum())  # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d66\u3001\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u90fd\u662f\u5b57\u6bcd**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"Name\"\n",
    "print(string.isalpha())  # True\n",
    "\n",
    "string = \"Firstname Lastname\"\n",
    "print(string.isalpha())  # False\n",
    "\n",
    "string = \"P4ssw0rd\"\n",
    "print(string.isalpha())  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d67\u3001\u6839\u636e\u53c2\u6570\u5220\u9664\u5b57\u7b26**\n",
    "\n",
    "\n",
    "\n",
    "\u4ece\u53f3\u4fa7\u5f00\u59cb\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"This is a sentence with       \"\n",
    "print(string.rstrip())  # \"This is a sentence with\"\n",
    "\n",
    "string = \"this here is a sentence\u2026..,,,,aaaaasd\"\n",
    "print(string.rstrip(\".,dsa\"))  # \"this here is a sentence\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u540c\u6837\u7684\uff0c\u5de6\u4fa7\u4e5f\u80fd\u64cd\u4f5c\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"ffffffffFirst\"\n",
    "print(string.lstrip(\"f\"))  # First\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d68\u3001\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6570\u5b57**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"seven\"\n",
    "print(string.isdigit())  # False\n",
    "\n",
    "string = \"1337\"\n",
    "print(string.isdigit())  # True\n",
    "\n",
    "string = \"5a\"\n",
    "print(string.isdigit())  # False\n",
    "\n",
    "string = \"2**5\"\n",
    "print(string.isdigit())  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d69\u3001\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u4e2d\u6587\u6570\u5b57**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 42673\n",
    "string = \"\u56db\u4e8c\u516d\u4e03\u4e09\"\n",
    "\n",
    "print(string.isdigit())  # False\n",
    "print(string.isnumeric())  # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d70\u3001\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u6240\u6709\u5355\u8bcd\u90fd\u662f\u5927\u5199\u5f00\u5934**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"This is a sentence\"\n",
    "print(string.istitle())  # False\n",
    "\n",
    "string = \"10 Python Tips\"\n",
    "print(string.istitle())  # True\n",
    "\n",
    "string = \"How to Print A String in Python\"\n",
    "# False\n",
    "print(string.istitle())\n",
    "\n",
    "string = \"PYTHON\"\n",
    "print(string.istitle())  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d71\u3001\u5728\u5143\u7ec4\u4e2d\u4f7f\u7528\u8d1f\u7d22\u5f15**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers = (1, 2, 3, 4)\n",
    "\n",
    "print(numbers[-1])  # 4\n",
    "print(numbers[-4])  # 1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d72\u3001\u5728\u5143\u7ec4\u4e2d\u5d4c\u5957\u5217\u8868\u548c\u5143\u7ec4**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mixed_tuple = ((\"a\"*10, 3, 4), ['first', 'second', 'third'])\n",
    "\n",
    "print(mixed_tuple[1])  # ['first', 'second', 'third']\n",
    "print(mixed_tuple[0])  # ('aaaaaaaaaa', 3, 4)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d73\u3001\u5feb\u901f\u7edf\u8ba1\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Besim\", \"Albert\", \"Besim\", \"Fisnik\", \"Meriton\"]\n",
    "\n",
    "print(names.count(\"Besim\"))  # 2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d74\u3001\u4f7f\u7528slice()\u83b7\u53d6\u5143\u7d20**\n",
    "\n",
    "\n",
    "\n",
    "\u4f7f\u7528slice()\u83b7\u53d6\u6700\u540en\u4e2a\u5143\u7d20\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "slicing = slice(-4, None)\n",
    "print(my_list[slicing])  # [4, 5, 6]\n",
    "\n",
    "print(my_list[-3])  # 4\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u4f7f\u7528slice()\u505a\u5207\u7247\u4efb\u52a1\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"Data Science\"\n",
    "\n",
    "slice_object = slice(5, None)\n",
    "print(string[slice_object])   # Science\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d75\u3001\u8ba1\u7b97\u5143\u7d20\u5728\u5143\u7ec4\u4e2d\u51fa\u73b0\u7684\u6b21\u6570**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = ('a', 1, 'f', 'a', 5, 'a')\n",
    "\n",
    "print(my_tuple.count('a'))  # 3\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d76\u3001\u83b7\u53d6\u5143\u7ec4\u4e2d\u5143\u7d20\u7684\u7d22\u5f15**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = ('a', 1, 'f', 'a', 5, 'a')\n",
    "\n",
    "print(my_tuple.index('f'))  #  2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d77\u3001\u6b65\u8fdb\u83b7\u5f97\u5143\u7ec4**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n",
    "\n",
    "print(my_tuple[::3])  # (1, 4, 7, 10)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d78\u3001\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u5b50\u5143\u7ec4**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n",
    "\n",
    "print(my_tuple[3:])  # (4, 5, 6, 7, 8, 9, 10)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d79\u3001\u5c06\u5217\u8868\u3001\u96c6\u5408\u3001\u5b57\u5178\u4e2d\u6240\u6709\u5143\u7d20\u5220\u9664**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1, 2, 3, 4]\n",
    "my_list.clear()\n",
    "print(my_list)  # []\n",
    "\n",
    "my_set = {1, 2, 3}\n",
    "my_set.clear()\n",
    "print(my_set)  # set()\n",
    "\n",
    "my_dict = {\"a\": 1, \"b\": 2}\n",
    "my_dict.clear()\n",
    "print(my_dict)  # {}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d80\u3001\u5408\u5e76\u96c6\u5408**\n",
    "\n",
    "\n",
    "\n",
    "\u4f7f\u7528union()\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u96c6\u5408\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_set = {4, 5, 6}\n",
    "second_set = {1, 2, 3}\n",
    "\n",
    "print(first_set.union(second_set))  # {1, 2, 3, 4, 5, 6}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd8\u53ef\u4ee5\u4f7f\u7528update()\u65b9\u6cd5\uff0c\u5c06\u7b2c\u4e8c\u4e2a\u96c6\u5408\u7684\u5143\u7d20\u63d2\u5165\u5230\u7b2c\u4e00\u4e2a\u96c6\u5408\u4e2d\u53bb\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_set = {4, 5, 6}\n",
    "second_set = {1, 2, 3}\n",
    "\n",
    "first_set.update(second_set)\n",
    "\n",
    "print(first_set)  # {1, 2, 3, 4, 5, 6}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d81\u3001\u5728\u51fd\u6570\u91cc\u8f93\u51fa\u7ed3\u679c**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_positive(number):\n",
    "    print(\"Positive\" if number > 0 else \"Negative\")  # Positive\n",
    "\n",
    "\n",
    "is_positive(-3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d82\u3001if\u8bed\u53e5\u4e2d\u7684\u591a\u4e2a\u6761\u4ef6**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "math_points = 51\n",
    "biology_points = 78\n",
    "physics_points = 56\n",
    "history_points = 72\n",
    "\n",
    "my_conditions = [math_points > 50, biology_points > 50,\n",
    "                 physics_points > 50, history_points > 50]\n",
    "\n",
    "if all(my_conditions):\n",
    "    print(\"Congratulations! You have passed all of the exams.\")\n",
    "else:\n",
    "    print(\"I am sorry, but it seems that you have to repeat at least one exam.\")\n",
    "# Congratulations! You have passed all of the exams.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d83\u3001\u5728\u4e00\u4e2aif\u8bed\u53e5\u4e2d\uff0c\u81f3\u5c11\u6ee1\u8db3\u591a\u4e2a\u6761\u4ef6\u4e2d\u7684\u4e00\u4e2a**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "math_points = 40\n",
    "biology_points = 78\n",
    "physics_points = 56\n",
    "history_points = 72\n",
    "\n",
    "my_conditions = [math_points > 50, biology_points > 50,\n",
    "                 physics_points > 50, history_points > 50]\n",
    "\n",
    "if any(my_conditions):\n",
    "    print(\"Congratulations! You have passed all of the exams.\")\n",
    "else:\n",
    "    print(\"I am sorry, but it seems that you have to repeat at least one exam.\")\n",
    "# Congratulations! You have passed all of the exams.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d84\u3001\u4efb\u4f55\u975e\u7a7a\u5b57\u7b26\u4e32\u90fd\u4e3aTrue**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(bool(\"Non empty\"))  # True\n",
    "print(bool(\"\"))  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d85\u3001\u4efb\u4f55\u975e\u7a7a\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u90fd\u4e3aTrue**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(bool([]))  # False\n",
    "print(bool(set([])))  # False\n",
    "\n",
    "print(bool({}))  # False\n",
    "print(bool({\"a\": 1}))  # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d86\u3001None\u3001False\u30010\u90fd\u4e3aFalse**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(bool(False))  # False\n",
    "print(bool(None))  # False\n",
    "print(bool(0))  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d87\u3001\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf**\n",
    "\n",
    "\n",
    "\n",
    "\u5728\u51fd\u6570\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"string\"\n",
    "\n",
    "\n",
    "def do_nothing():\n",
    "  string = \"inside a method\"\n",
    "\n",
    "\n",
    "do_nothing()\n",
    "\n",
    "print(string)  # string\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u53ef\u901a\u8fc7\u4fee\u9970\u7b26global\uff0c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"string\"\n",
    "\n",
    "\n",
    "def do_nothing():\n",
    "    global string\n",
    "    string = \"inside a method\"\n",
    "\n",
    "\n",
    "do_nothing()\n",
    "\n",
    "print(string)  # inside a method\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d88\u3001\u8ba1\u7b97\u5b57\u7b26\u4e32\u6216\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6570\u91cf**\n",
    "\n",
    "\n",
    "\n",
    "\u4f7f\u7528collections\u4e2d\u7684Counter\u8ba1\u7b97\u5b57\u7b26\u4e32\u6216\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "\n",
    "result = Counter(\"Banana\")\n",
    "print(result)  # Counter({'a': 3, 'n': 2, 'B': 1})\n",
    "\n",
    "\n",
    "result = Counter([1, 2, 1, 3, 1, 4, 1, 5, 1, 6])\n",
    "print(result)  # Counter({1: 5, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1})\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d89\u3001\u68c0\u67e52\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u76f8\u540c**\n",
    "\n",
    "\n",
    "\n",
    "\u53ef\u4ee5\u4f7f\u7528Counter()\u65b9\u6cd5\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "\n",
    "\n",
    "def check_if_anagram(first_string, second_string):\n",
    "    first_string = first_string.lower()\n",
    "    second_string = second_string.lower()\n",
    "    return Counter(first_string) == Counter(second_string)\n",
    "\n",
    "\n",
    "print(check_if_anagram('testinG', 'Testing'))  # True\n",
    "print(check_if_anagram('Here', 'Rehe'))  # True\n",
    "print(check_if_anagram('Know', 'Now'))  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u53ef\u4ee5\u4f7f\u7528sorted()\u65b9\u6cd5\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check_if_anagram(first_word, second_word):\n",
    "    first_word = first_word.lower()\n",
    "    second_word = second_word.lower()\n",
    "    return sorted(first_word) == sorted(second_word)\n",
    "\n",
    "\n",
    "print(check_if_anagram(\"testinG\", \"Testing\"))  # True\n",
    "print(check_if_anagram(\"Here\", \"Rehe\"))  # True\n",
    "print(check_if_anagram(\"Know\", \"Now\"))  # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d90\u3001\u4f7f\u7528itertools\u4e2d\u7684count\u8ba1\u7b97\u5143\u7d20\u7684\u6570\u91cf**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import count\n",
    "\n",
    "my_vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']\n",
    "\n",
    "current_counter = count()\n",
    "\n",
    "string = \"This is just a sentence.\"\n",
    "\n",
    "for i in string:\n",
    "    if i in my_vowels:\n",
    "        print(f\"Current vowel: {i}\")\n",
    "        print(f\"Number of vowels found so far: {next(current_counter)}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8f93\u51fa\u5982\u4e0b\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Current vowel: i\n",
    "Number of vowels found so far: 0\n",
    "Current vowel: i\n",
    "Number of vowels found so far: 1\n",
    "Current vowel: u\n",
    "Number of vowels found so far: 2\n",
    "Current vowel: a\n",
    "Number of vowels found so far: 3\n",
    "Current vowel: e\n",
    "Number of vowels found so far: 4\n",
    "Current vowel: e\n",
    "Number of vowels found so far: 5\n",
    "Current vowel: e\n",
    "Number of vowels found so far: 6\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d91\u3001\u5bf9\u5b57\u7b26\u4e32\u6216\u5217\u8868\u7684\u5143\u7d20\u8fdb\u884c\u6b21\u6570\u6392\u5e8f**\n",
    "\n",
    "\n",
    "\n",
    "collections\u6a21\u5757\u7684Counter()\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u4e0d\u4f1a\u6839\u636e\u5143\u7d20\u7684\u9891\u7387\u5bf9\u5b83\u4eec\u8fdb\u884c\u6392\u5e8f\u7684\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "\n",
    "result = Counter([1, 2, 3, 2, 2, 2, 2])\n",
    "print(result)  # Counter({2: 5, 1: 1, 3: 1})\n",
    "print(result.most_common())  # [(2, 5), (1, 1), (3, 1)]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "map()\u51fd\u6570\u5c06\u7ed9\u5b9a\u51fd\u6570\u5e94\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61(\u5217\u8868\u3001\u5143\u7ec4\u7b49)\uff0c\u7136\u540e\u8fd4\u56de\u7ed3\u679c(map\u5bf9\u8c61)\u3002\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**\u258d92\u3001\u67e5\u627e\u5217\u8868\u4e2d\u51fa\u73b0\u9891\u7387\u6700\u9ad8\u7684\u5143\u7d20**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = ['1', 1, 0, 'a', 'b', 2, 'a', 'c', 'a']\n",
    "\n",
    "print(max(set(my_list), key=my_list.count))  # a\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d93\u3001copy()\u548cdeepcopy()\u7684\u533a\u522b**\n",
    "\n",
    "\n",
    "\n",
    "\u6d45\u62f7\u8d1d: \u62f7\u8d1d\u7236\u5bf9\u8c61\uff0c\u4f46\u662f\u4e0d\u4f1a\u62f7\u8d1d\u5bf9\u8c61\u7684\u5185\u90e8\u7684\u5b50\u5bf9\u8c61\u3002\n",
    "\n",
    "\u6df1\u62f7\u8d1d: \u62f7\u8d1d\u7236\u5bf9\u8c61. \u4ee5\u53ca\u5176\u5185\u90e8\u7684\u5b50\u5bf9\u8c61\u3002\n",
    "\n",
    "\n",
    "\n",
    "\u4e0b\u9762\u662f\u4e00\u4e2acopy()\u7684\u4f8b\u5b50\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_list = [[1, 2, 3], ['a', 'b', 'c']]\n",
    "\n",
    "second_list = first_list.copy()\n",
    "\n",
    "first_list[0][2] = 831\n",
    "\n",
    "print(first_list)  # [[1, 2, 831], ['a', 'b', 'c']]\n",
    "print(second_list)  # [[1, 2, 831], ['a', 'b', 'c']]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u8fd9\u91cc\u662f\u4e00\u4e2adeepcopy()\u7684\u4f8b\u5b50\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "\n",
    "first_list = [[1, 2, 3], ['a', 'b', 'c']]\n",
    "\n",
    "second_list = copy.deepcopy(first_list)\n",
    "\n",
    "first_list[0][2] = 831\n",
    "\n",
    "print(first_list)  # [[1, 2, 831], ['a', 'b', 'c']]\n",
    "print(second_list)  # [[1, 2, 3], ['a', 'b', 'c']]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d94\u3001\u8bbf\u95ee\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u907f\u514d\u62a5\u9519**\n",
    "\n",
    "\n",
    "\n",
    "\u5982\u679c\u4f60\u60f3\u8bbf\u95ee\u5b57\u5178\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u4ee3\u7801\u4f1a\u62a5\u9519\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_dictonary = {\"name\": \"Name\", \"surname\": \"Surname\"}\n",
    "print(my_dictonary[\"age\"])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u9519\u8bef\u5982\u4e0b\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "KeyError: 'age'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528defaultdict()\uff0c\u4ee3\u7801\u5c06\u4e0d\u4f1a\u62a5\u9519\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "\n",
    "my_dictonary = defaultdict(str)\n",
    "my_dictonary['name'] = \"Name\"\n",
    "my_dictonary['surname'] = \"Surname\"\n",
    "\n",
    "print(my_dictonary[\"age\"])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d95\u3001\u6784\u5efa\u8fed\u4ee3\u5668**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class OddNumbers:\n",
    "    def __iter__(self):\n",
    "        self.a = 1\n",
    "        return self\n",
    "\n",
    "    def __next__(self):\n",
    "        x = self.a\n",
    "        self.a += 2\n",
    "        return x\n",
    "\n",
    "\n",
    "odd_numbers_object = OddNumbers()\n",
    "iterator = iter(odd_numbers_object)\n",
    "\n",
    "print(next(iterator))  # 1\n",
    "print(next(iterator))  # 3\n",
    "print(next(iterator))  # 5\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d96\u3001\u5220\u9664\u5217\u8868\u7684\u91cd\u590d\u9879**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_set = set([1, 2, 1, 2, 3, 4, 5])\n",
    "print(list(my_set))  # [1, 2, 3, 4, 5]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d97\u3001\u6253\u5370\u6a21\u5757\u7684\u5b89\u88c5\u4f4d\u7f6e**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas\n",
    "\n",
    "print(pandas)  # <module 'torch' from '/Users/...'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d98\u3001\u4f7f\u7528not in\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5217\u8868\u4e2d**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odd_numbers = [1, 3, 5, 7, 9]\n",
    "even_numbers = []\n",
    "\n",
    "for i in range(9):\n",
    "    if i not in odd_numbers:\n",
    "        even_numbers.append(i)\n",
    "\n",
    "print(even_numbers)  # [0, 2, 4, 6, 8]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d99\u3001sort()\u548csorted()\u7684\u533a\u522b**\n",
    "\n",
    "\n",
    "\n",
    "sort()\uff1a\u5bf9\u539f\u59cb\u5217\u8868\u8fdb\u884c\u6392\u5e8f\n",
    "\n",
    "sorted()\uff1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "groceries = ['milk', 'bread', 'tea']\n",
    "\n",
    "new_groceries = sorted(groceries)\n",
    "# new_groceries = ['bread', 'milk', 'tea']\n",
    "\n",
    "print(new_groceries)\n",
    "\n",
    "# groceries = ['milk', 'bread', 'tea']\n",
    "print(groceries)\n",
    "\n",
    "groceries.sort()\n",
    "\n",
    "# groceries = ['bread', 'milk', 'tea']\n",
    "print(groceries)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**\u258d100\u3001\u4f7f\u7528uuid\u6a21\u5757\u751f\u6210\u552f\u4e00ID**\n",
    "\n",
    "\n",
    "\n",
    "UUID\u4ee3\u8868\u552f\u4e00\u6807\u8bc6\u7b26\u3002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import uuid\n",
    "\n",
    "# \u6839\u636e\u4e3b\u673aID\u3001\u5e8f\u5217\u53f7\u548c\u5f53\u524d\u65f6\u95f4\u751f\u6210UUID\n",
    "print(uuid.uuid1())  # 308490b6-afe4-11eb-95f7-0c4de9a0c5af\n",
    "\n",
    "# \u751f\u6210\u4e00\u4e2a\u968f\u673aUUID\n",
    "print(uuid.uuid4())  # 93bc700b-253e-4081-a358-24b60591076a\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}